package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sql.record.*;
import sql.DBManagment;

/**
 * Servlet implementation class register
 */
public class register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public register() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User registeringUser = new User();

		//getting data from form
		registeringUser.userName = request.getParameter("username");
		registeringUser.surName = request.getParameter("surName");
		registeringUser.givenName = request.getParameter("givenName");
		registeringUser.phoneNumber = request.getParameter("phoneNumber");
		registeringUser.password = request.getParameter("password1");
		registeringUser.type = 0;
		//adding to DB
		List<TableRecord> regList = new LinkedList<TableRecord>();
		regList.add(registeringUser);
		PrintWriter out = response.getWriter();
		try {
			//inserting to DB
			DBManagment.insertRecord(regList);
			//setting session
			request.getSession().setAttribute("loggedUser", registeringUser.userName);
			//output
			out.print("<firefoxsucks>registered</firefoxsucks>");
		} catch (Exception e) {
			//output
			out.print("<firefoxsucks>unregistered</firefoxsucks>");
		}
		out.close();
	}

}
